package cn.nkjobsearch.action;

import java.util.ArrayList;

import com.opensymphony.xwork2.ActionSupport;

import cn.nkjobsearch.publicClass.SearchAndSort;
import cn.nkjobsearch.search.Search;
import cn.nkjobsearch.jena.NKJSJena;
import cn.nkjobsearch.mysql.MysqlConn;
import cn.nkjobsearch.convert.KeywordIndex;
import cn.nkjobsearch.convert.keywordCode;

public class AjaxKeywordAction extends ActionSupport
{
	int[] keywordTemp;
	int[][] rs;
	private String keywordList;
    private String result = "";
    private MysqlConn mysql = null;

    // ajax请求参数赋值
    public void setKeywordList(String keywordList) 
    {
        this.keywordList = keywordList;
    }
    // ajax返回结果
    public String getResult() 
    {
        return result;
    }
    public String execute() 
    {
    	KeywordIndex ki = new KeywordIndex();//实例化一个KeywordIndex对象 ki
    	this.keywordTemp = ki.getKeywordFromUI(keywordList);
    	
    	rs = getKeywordFromJena(this.keywordTemp);

    	this.result +="<br>输入关键词：【"+keywordList+"】<br>关键词扩展：";
    	if( 0 == this.rs.length)
			this.result +=" 没有相关扩展信息！";
		else
		{
	    	for (int i = 0; i < this.rs.length; i++) 
	    	{
				int[] o = this.rs[i];
				this.result +=" <input type=checkbox name=\"extendKeyword\" value=\""+o[0]+"_"+o[1]+"_"+o[2]+"\"> "
							+ keywordCode.getKeywordViaCode(o[0]).toString()
							+" ";
			}
		}
    	this.rs = null;
        return SUCCESS;
    }
    private int[][] getKeywordFromJena(int[] keywordIdList) {
		ArrayList<int[]> keywordFromJena = new ArrayList<int[]>();// {id, relationship, fromKW_ID}
		NKJSJena jn = new NKJSJena();
		for(int i=0; i<keywordIdList.length; i++) {
			ArrayList<int[]> rs = jn.findKeyword(keywordIdList[i]);
			for(int j=0; j<rs.size(); j++) {
				keywordFromJena.add(new int[]{rs.get(j)[0], rs.get(j)[1], keywordIdList[i]});
			}
		}
		int[][] list = new int[keywordFromJena.size()][3];
		for(int i=0; i<keywordFromJena.size(); i++) {
			list[i][0] = keywordFromJena.get(i)[0];
			list[i][1] = keywordFromJena.get(i)[1];
			list[i][2] = keywordFromJena.get(i)[2];
		}
		SearchAndSort.QuickSort(list, 1, 3, 0, list.length - 1, true);
		return list;
	}
}
